Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
Django problem with missing " underscore" in url

I am a beginner and need a little support. Everything works in the application, but when in the form it typing "new york", "_" is missing in the url.

My code is

def index(request):
    if request.method == 'POST':
        city = request.POST['city']
   
        if city == '':
            return redirect('index')
        res = urllib.request.urlopen(f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=1b79ea7b1251b76ccda81d6bd').read()
       

        json_data = json.loads(res)
        datas = {
            "country_code" : str(json_data['sys']['country']),
            "cordinate": str(json_data['coord']['lon'])+ ''+ 
            str(json_data['coord']['lat']),
            "temp": str(int(json_data['main']['temp']-273)) + 'C',
            "pressure" : str(json_data['main']['pressure']),
            "humidity" : str(json_data['main']['humidity']),

        }


In response receives "

InvalidURL at /
URL can't contain control characters. '/data/2.5/weather?q=bielsko biala&appid=1b79ea7b1251b76cc0b41a81d6bd' (found at least ' ')

I am looking for solutions on the Internet but I cannot solve it myself. Thank you very much for all your help

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You just need to replace spaces in city with the encoded values.

Try this:

import urllib.parse    
res = urllib.request.urlopen(f'http://api.openweathermap.org/data/2.5/weather?q={urllib.parse.quote(city)}&appid=1b79ea7b1251b76ccda81d6bd').read()

this is to encode city in the URL.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda